'use strict';

const BaseModel = require('./BaseModel');

/**
 * 操作日志 表信息
 */
class SysOperationLogsModel extends BaseModel {

  constructor() {
    super('sys_operation_logs', 'id');
    this.entity = class entity {
      constructor() {
        this.id = null;
        this.operatorId = null;
        this.operatorName = null;
        this.functionName = null;
        this.type = null;
        this.des = null;
        this.time = null;
      }
    };
  }
  // 查询日志
  async findSysOperationLogs(app, options) {
    const pageSize = parseInt(options.pageSize || 10);
    let pageIndex = parseInt(options.pageIndex || 0);
    if (pageIndex - 1 <= 0) {
      pageIndex = 1;
    }
    let sql = ' select * from sys_operation_logs where 1= 1 ';
    if (options.operatorName) {
      sql += ` and operator_name like \'%${options.operatorName}%\' `;
    }
    if (options.functionName) {
      sql += ` and function_name like \'%${options.functionName}%\' `;
    }
    if (options.type) {
      sql += ` and type like \'%${options.type}%\' `;
    }
    if (options.startDate && options.endDate) {
      sql += ` and time >= '${options.startDate}' and time < '${options.endDate}' `;
    }
    const countSql = `select count(1) logCount from (${sql}) logs`;
    const totalCount = await app.mysql.query(countSql);
    const pageCount = Math.ceil(totalCount[0].logCount / pageSize);
    sql += ` order by time desc limit ${(pageIndex - 1) * pageSize},${pageSize}`;
    const results = await app.mysql.query(sql);
    const systemLogList = [];
    if (results.length > 0) {
      for (const dbentity of results) {
        let systemLog = new this.entity();
        systemLog = this.transformation_Entity(dbentity);
        systemLogList.push(systemLog);
      }
    }
    return {
      pageIndex,
      pageSize,
      pageCount,
      dataCount: totalCount[0] && totalCount[0].logCount || 0,
      systemLogList,
    };
  }
}

module.exports = SysOperationLogsModel;
